package com.xhwl.logistics.converter.mapstruct;

import com.google.common.base.Strings;
import com.xhwl.common.dto.cent.vehicle.feign.VehicleCarportQuery;
import com.xhwl.logistics.bo.VehicleCarportBO;
import com.xhwl.logistics.bo.VehicleCarportNanTouBO;
import com.xhwl.logistics.bo.VehicleParkingSpaceBO;
import com.xhwl.logistics.bo.input.QueryVehicleCarportBO;
import com.xhwl.logistics.dto.http.*;
import com.xhwl.logistics.dto.query.VehicleCarportDTO;
import com.xhwl.logistics.vo.VehicleCarSpaceCoSonVO;
import com.xhwl.logistics.vo.VehicleCarportNanTouVO;
import com.xhwl.logistics.vo.VehicleCarportSecondVO;
import com.xhwl.logistics.vo.VehicleCarportVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.NullValueMappingStrategy;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;

@Mapper(nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT)
public interface VehicleCarportConverter {

    @Mapping(source = "totalPlace", target = "totalNumber")
    @Mapping(source = "leftPlace", target = "freeNumber")
    VehicleCarportBO carportHaiKangDTO2VehicleCarportBO(CarportHaiKangDTO carportHaiKangDTO);

    @Mapping(source = "freeSpaceNumber", target = "freeNumber")
    VehicleCarportBO freeSpaceNumberKeTopDTO2VehicleCarportBO(FreeSpaceNumberKeTopDTO freeSpaceNumberKeTopDTO);

    @Mapping(source = "total", target = "totalNumber")
    @Mapping(source = "remain", target = "freeNumber")
    VehicleCarportBO carportCheAnDTO2VehicleCarportBO(CarportCheAnDTO carportCheAnDTO);

    //************Nan Tou************
    //************BO <- Http DTO************
    VehicleCarportNanTouBO carportNanTouDTO2VehicleCarportBO(CarportNanTouDTO carportNanTouDTO);
    //************BO <- Http DTO************
    //************Nan Tou************

    @Mapping(source = "totalSpace", target = "totalNumber")
    @Mapping(source = "restSpace", target = "freeNumber")
    VehicleCarportBO carportJieShunDTO2VehicleCarportBO(CarportJieShunDTO carportJieShunDTO);

    //************Query BO <- Controller DTO************
    QueryVehicleCarportBO vehicleCarportDTO2QueryVehicleCarportBO(VehicleCarportDTO vehicleCarportDTO);
    //************Query BO <- Controller DTO************


    //************Query BO <- Feign Query************
    QueryVehicleCarportBO vehicleCarportQuery2QueryVehicleCarportBO(VehicleCarportQuery vehicleCarportQuery);
    //************Query BO <- Feign Query************


    //*********************VO <- BO*********************
    //************VO************
    default VehicleCarportVO vehicleCarportBO2VehicleCarportVO(VehicleCarportBO vehicleCarportBO, String parkingLotName) {
        VehicleCarportVO vehicleCarportVO = toVehicleCarportVO(vehicleCarportBO, parkingLotName);
        if (vehicleCarportBO instanceof VehicleCarportNanTouBO) {
            VehicleCarportNanTouBO vehicleCarportNanTouBO = (VehicleCarportNanTouBO)vehicleCarportBO;
            return toVehicleCarportNanTouVO(vehicleCarportVO, vehicleCarportNanTouBO, parkingLotName);
        } else {
            return vehicleCarportVO;
        }
    }
    //************VO************

    //************Cent VO************
    default VehicleCarportVO toVehicleCarportVO(VehicleCarportBO vehicleCarportBO, String parkingLotName) {
        VehicleCarportVO vehicleCarportVO = new VehicleCarportVO();
        vehicleCarportVO.setTotalNumber(vehicleCarportBO.getTotalNumber());
        vehicleCarportVO.setFreeNumber(vehicleCarportBO.getFreeNumber());
        if (Strings.isNullOrEmpty(parkingLotName)) {
            vehicleCarportVO.setParkingLotName("All ParkingLot");
        } else {
            vehicleCarportVO.setParkingLotName(parkingLotName);
        }
        return vehicleCarportVO;
    }
    //************Cent VO************

    //************Nan Tou VO************
    default VehicleCarportNanTouVO toVehicleCarportNanTouVO(VehicleCarportVO vehicleCarportVO, VehicleCarportNanTouBO vehicleCarportNanTouBO, String parkingLotName) {
        VehicleCarportNanTouVO vehicleCarportNanTouVO = new VehicleCarportNanTouVO(vehicleCarportVO);
        vehicleCarportNanTouVO.setTemporaryCardNumber(vehicleCarportNanTouBO.getTemporaryCardNumber());
        vehicleCarportNanTouVO.setMonthCardANumber(vehicleCarportNanTouBO.getMonthCardANumber());
        vehicleCarportNanTouVO.setMonthCardBNumber(vehicleCarportNanTouBO.getMonthCardBNumber());
        return vehicleCarportNanTouVO;
    }
    //************Nan Tou VO************
    //*********************VO <- BO*********************


    //************Co Son VO************
    default VehicleCarSpaceCoSonVO toVehicleCarSpaceCoSonVO(VehicleParkingSpaceBO vehicleParkingSpaceBO) {
        VehicleCarSpaceCoSonVO vehicleCarSpaceCoSonVO = new VehicleCarSpaceCoSonVO();
        BeanUtils.copyProperties(vehicleParkingSpaceBO,vehicleCarSpaceCoSonVO);
            return vehicleCarSpaceCoSonVO;

    }
    //************Co Son VO************
    //*********************VO <- BO*********************

    //************Co Son Query BO************
    @Mapping(source = "totalSpaces", target = "totalNumber")
    @Mapping(source = "remaiSpaces", target = "freeNumber")
    VehicleCarportBO carportCoSonDTO2VehicleCarportBO(CarportCoSonDTO carportCoSonDTO);

    //*********************Query BO <- Controller DTO*********************

    //*******************VO <- BO****************
    default VehicleCarportSecondVO toVehicleCarportSecondVO(VehicleCarportBO vehicleCarportBO){
        VehicleCarportSecondVO vehicleCarportSecondVO = new VehicleCarportSecondVO();
        vehicleCarportSecondVO.setTotalNumber(vehicleCarportBO.getTotalNumber());
        vehicleCarportSecondVO.setFreeNumber(vehicleCarportBO.getFreeNumber());
        if(vehicleCarportBO.getTotalNumber()!=null&&vehicleCarportBO.getFreeNumber()!=null&&vehicleCarportBO.getTotalNumber()!=0){
            double freeRateDouble = vehicleCarportBO.getFreeNumber().doubleValue() / vehicleCarportBO.getTotalNumber().doubleValue();
            BigDecimal freeRateDecimal = new BigDecimal(freeRateDouble * 100);
            String freeRateStr = String.valueOf(freeRateDecimal.setScale(2, BigDecimal.ROUND_DOWN).doubleValue());
            int index = freeRateStr.lastIndexOf(".");
            String substring = freeRateStr.substring(index);
            if(substring.length()==2){
                vehicleCarportSecondVO.setFreeRate(freeRateStr+"0");
            }else{
                vehicleCarportSecondVO.setFreeRate(freeRateStr+"");
            }
        }else{
            vehicleCarportSecondVO.setFreeRate("0.00");
        }
        return vehicleCarportSecondVO;
    }
    //*******************VO <- BO****************
}
